package com.game.serivce;

import net.ServerHandler;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;




import com.data.serivce.Impl.PlayerDBService;
import com.data.spring.BeanFactory;
import com.dictionary.DictionaryTabel;
import com.game.model.DeskPlayer;

public class PlayerService {
	private Logger logger = Logger.getLogger(PlayerService.class.getName());
	public DeskPlayer deskPlayer;//玩家实体
	private IoSession session = null;

//	public PlayerService(JSONObject json) {
//		this.deskPlayer = new DeskPlayer();
//	}
	
	public PlayerService(JSONObject json) {
		setPlayer(new DeskPlayer(json));
	}

	public DeskPlayer getPlayer() {
		return deskPlayer;
	}

	public void setPlayer(DeskPlayer deskPlayer) {
		this.deskPlayer = deskPlayer;
	}
	public IoSession getSession() {
		return session;
	}
	public void setSession(IoSession session) {
		this.session = session;
	}
	
	public long getPropNum(int propId) {
		if (deskPlayer.getBagInfo().containsKey(String.valueOf(propId)))
			return deskPlayer.getBagInfo().getLong(String.valueOf(propId));
		
		return 0;
	}
	
	public void changePropNum(int propId, long winMoney) {
		if (deskPlayer.getBagInfo().containsKey(String.valueOf(propId)))
			deskPlayer.getBagInfo().put(String.valueOf(propId), deskPlayer.getBagInfo().getLong(String.valueOf(propId)) + winMoney);
		else
			deskPlayer.getBagInfo().put(String.valueOf(propId), winMoney);
	}
	
	public void sendMessage(String message) {
		if (session != null) {
			try {
				logger.info(deskPlayer.getPlayerId() + "," + message);
				session.write(message);
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
		}
	}
	
	public void sendMessage(int messageHead, String messageContent) {
		sendMessage( messageHead + "@" + messageContent + "@&");
	}
	/**
	 * 玩家结算
	 * @param propId
	 * @param changeNum
	 * @param changeType
	 * @return
	 */
//	public long jieSuan(int propId, long changeNum) {
//		if (changeNum > 0) {
//			changeNum = (long) (changeNum * (1 - ServerHandler.gameRoomServices[deskPlayer.getRoomNum()].configRoom.taxRate));
//		}
//		
//		if (deskPlayer.getPlayerType() == DictionaryTabel.PlayerType_People) {	
//			changeNum = PlayerDBService.getInstance().updateBagInfo((DeskService) this, propId, changeNum);
//		}
//		return changeNum;
//	}
	public void getdiankong(long playerId) {
		try {
			// 获取点控
			deskPlayer.setPlayerDiankong(BeanFactory.PlayerService.getDianKongInfoLast(Long.valueOf(playerId)));

		} catch (Exception e) {
			e.printStackTrace();
			logger.error("commonExceotion", e);
		}
	}
	public void clearEndJu() {
		getPlayer().setState(DictionaryTabel.player_Idle);
	}
}
